package com.zhilei.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhilei.base.BaseInfoProperties;
import com.zhilei.mapper.ResumeExpectMapper;
import com.zhilei.pojo.ResumeExpect;
import com.zhilei.pojo.bo.EditResumeExpectBO;
import com.zhilei.pojo.vo.EditResumeExpectVO;
import com.zhilei.service.ResumeExpectService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 求职期望表 服务类
 * </p>
 *
 * @author 志磊
 * @since 2025-02-05
 */
@Slf4j
@Service
public class ResumeExpectServiceImpl extends BaseInfoProperties implements ResumeExpectService {

    @Autowired
    private ResumeExpectMapper resumeExpectMapper;

    /**
     * 保存编辑求职期望
     *
     * @param editEducationBO 求职期望实体类
     * @return
     */
    @Transactional
    @Override
    public void editJobExpect(EditResumeExpectBO editEducationBO) {
        String id = editEducationBO.getId();
        ResumeExpect resumeExpect = new ResumeExpect();
        BeanUtils.copyProperties(editEducationBO, resumeExpect);
        if (StringUtils.isBlank(id)) {
            // TODO 没有id就是新增了
            resumeExpect.setCreateTime(LocalDateTime.now());
            resumeExpect.setUpdatedTime(LocalDateTime.now());
            resumeExpectMapper.insert(resumeExpect);
        } else {
            // TODO 编辑了
            resumeExpect.setUpdatedTime(LocalDateTime.now());
            resumeExpectMapper.updateById(resumeExpect);
        }
    }

    /**
     * 删除求职期望
     *
     * @param resumeExpectId 简历期望id
     * @param userId         用户id
     * @return
     */
    @Transactional
    @Override
    public void deleteMyResumeExpect(String resumeExpectId, String userId) {
        resumeExpectMapper.delete(new QueryWrapper<ResumeExpect>()
                .eq("id", resumeExpectId)
                .eq("user_id", userId));
    }

    /**
     * 根据期望得id查询期望信息
     *
     * @param resumeId
     * @return
     */
    @Override
    public List<EditResumeExpectVO> getMyResumeExpectList(String resumeId, String userId) {
        // 根据 resumeId 查询所有符合条件的 ResumeExpect 记录
        List<ResumeExpect> idList = resumeExpectMapper.selectList(new QueryWrapper<ResumeExpect>()
                .eq("resume_id", resumeId)
                .eq("user_id", userId));

        // 用于存储转换后的 EditResumeExpectVO 对象
        List<EditResumeExpectVO> editResumeExpectVOList = new ArrayList<>();

        // 遍历 idList，进行对象转换
        idList.forEach(resumeExpect -> {
            EditResumeExpectVO editResumeExpectVO = new EditResumeExpectVO();
            BeanUtils.copyProperties(resumeExpect, editResumeExpectVO);
            editResumeExpectVOList.add(editResumeExpectVO); // 添加到集合中
        });

        // 返回转换后的集合
        return editResumeExpectVOList;
    }

}
